import {
  Body,
  Controller,
  Get,
  Post,
  Query,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOkResponse,
  ApiOperation,
  ApiQuery,
  ApiResponse,
} from '@nestjs/swagger';
import { CreatePairDto, UpdatePairDto } from '../dto/create-pair.dto';
import logger from 'src/common/utils/log';
import { ResponsePairDto } from '../dto/response-pair.dto';
import { PlayerSignupService } from '../service/player-signup.service';
import { playerSignupTypeEnum } from 'src/common/constant/enums';
import { SearchPairResponseDto, SearchPlayerDto, SearchPlayerResponseDto } from '../dto/search-player.dto';
import { Auth } from 'src/common/decorators/auth.decorator';
import { RequirePermission } from 'src/common/decorators/permission.decorator';
@Auth()
@ApiBearerAuth() //
@Controller('player-signup/pair')
export class PlayerSignupPairController {
  constructor(private readonly playerSignupService: PlayerSignupService) {}
  @RequirePermission('1002002')
  @Post('add')
  @ApiOperation({ summary: '添加双打组合信息' })
  @ApiBody({ type: CreatePairDto })
  @ApiOkResponse({ description: '添加双打组合信息成功' })
  @ApiResponse({ status: 400, description: '无效的请求数据' })
  @UsePipes(new ValidationPipe({ transform: true }))
  async add(@Body() createPairDto: CreatePairDto) {
    return await this.playerSignupService.addPair(createPairDto);
  }
  @RequirePermission('1002003')
  @Post('update')
  @UsePipes(new ValidationPipe({ transform: true }))
  @ApiOperation({ summary: '更新双打组合信息' })
  @ApiBody({ type: UpdatePairDto })
  @ApiOkResponse({ description: '双打组合信息更新成功' })
  @ApiResponse({ status: 400, description: '无效的请求数据' })
  async update(@Body() updatePairDto: UpdatePairDto) {
    return await this.playerSignupService.updatePair(updatePairDto);
  }
  @RequirePermission('1002004')
  @Get('delete')
  @ApiOperation({ summary: '删除双打组合信息' })
  @ApiQuery({
    name: 'eventId',
    type: Number,
    isArray: true,
    required: true,
    description: '比赛项目ID',
  })
  @ApiQuery({
    name: 'pairName',
    type: Number,
    required: true,
    description: '双打组合名称',
  })
  @ApiOkResponse({ description: '双打组合信息删除成功' })
  @ApiResponse({ status: 400, description: '无效的查询参数' })
  delete(
    @Query('eventId') eventId: number[],
    @Query('pairName') pairName: string,
  ) {
    logger.info(`${eventId} ${pairName}`);
    this.playerSignupService.deletePair(eventId, pairName);
  }

  @RequirePermission('1002001')
  @Post('search')
  @ApiOperation({ summary: '搜索双打组合报名信息' })
  @ApiBody({ type: SearchPlayerDto })
  @ApiOkResponse({
    description: '搜索成功',
    type: [SearchPairResponseDto],
  })
  @ApiResponse({ status: 400, description: '无效的查询参数' })
  async search(
    @Body() reqParam: SearchPlayerDto,
  ): Promise<SearchPairResponseDto> {
    // return null;
    const {
      param: { categoryId, eventIds, name: playerName },
      page,
      size,
    } = reqParam;
    const normalizedEventIds = Array.isArray(eventIds)
      ? eventIds
      : eventIds !== undefined
        ? [eventIds]
        : [];
    logger.info(
      `开始 playerSignupController.search categoryId： ${categoryId} eventIds: ${normalizedEventIds} playerName: ${playerName}`,
    );
    const signupPlayers = await this.playerSignupService.searchPair(
      page,
      size,
      {
        categoryId,
        eventIds: normalizedEventIds,
        playerName,
      },
    );
    return signupPlayers;
  }
}
